package com.lgy.springcloud.controller;

import com.lgy.springcloud.common.util.CommonResult;
import com.lgy.springcloud.common.util.R;
import com.lgy.springcloud.entity.Payment;
import com.lgy.springcloud.service.PaymentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@Api("payment测试接口")
public class PaymentController {
    @Autowired
    PaymentService paymentService;
    @Value("${server.port}")
    private String serverPort;

    @GetMapping("/pay")
    @ApiOperation("获取流水账信息")
    public R list(){
        List<Payment> list = paymentService.findAll();
        return R.ok("查询ok,serverPort:"+serverPort).put("paymentList",list);
    }

    @GetMapping("/pay/{id}")
    @ApiOperation("根据id获取流水信息")
    public R findById(@PathVariable("id") Long id){
        Payment payment = paymentService.findById(id);
        if (payment==null){
            return R.error("no record");
        }
        return R.ok("查询ok,serverPort:"+serverPort).put("payment",paymentService.findById(id));
    }

    @PostMapping("/pay")
    @ApiOperation("添加账单")
    public R save(@RequestBody Payment payment){
        int result = paymentService.save(payment);
        log.info("==================>插入结果：{}",result);
        if(result>0){
            return R.ok("查询ok,serverPort:"+serverPort);
        }
        return R.error(-1,"Failed");
    }

    @PutMapping("/pay")
    @ApiOperation("修改账单")
    public R update(@RequestBody Payment payment){
        int result = paymentService.updateById(payment);
        log.info("==================>更新结果：{}",result);
        if(result>0){
            return R.ok("查询ok,serverPort:"+serverPort);
        }
        return R.error(-1,"Failed");
    }
}
